package cm.studapp;

import cm.studapp.database.DbManager;
import cm.studapp.fragments.SemesterSelect;
import cm.studapp.helpers.ButtonHelper;
import cm.studapp.helpers.TimeHelper;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class CreateSchedule extends FragmentActivity {
	
	private Button addNew;
	private ButtonHelper bHelper;
	private LinearLayout actParent;
	private TimeHelper th;
	private DbManager db;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_schedule_main);
		
		//Mostrar el fragmento si no hay semestre configurado:
		db = new DbManager(this);
		th = new TimeHelper();
		if(!db.isSemesterSetted()){
		DialogFragment newFragment = SemesterSelect.newInstance();
		newFragment.show(getSupportFragmentManager(), "lol");
		}
		
		bHelper = new ButtonHelper(this);
		initViews();

	}
	
	public void onResume(){
		super.onResume();
		loadActCreated();
	}
	
	private void loadActCreated() {
		// TODO Auto-generated method stub
		Cursor actCreated = db.getAllActRep();
		actParent.removeAllViews();
		while(actCreated.moveToNext()){
			LinearLayout actElem = (LinearLayout)getLayoutInflater().inflate(R.layout.create_schedule_module_elem, null);
			LinearLayout.LayoutParams paramActElem = new
					LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			paramActElem.leftMargin = 10;
			paramActElem.rightMargin = 10;
			paramActElem.bottomMargin = 2;
			actElem.setLayoutParams(paramActElem);
			TextView actName =(TextView)actElem.getChildAt(0);
			int actId = (int) actCreated.getInt(0); 
			actName.setText(actCreated.getString(1));
			LinearLayout modsParent = (LinearLayout)actElem.getChildAt(1);
			Cursor mod = db.getWeekModules(actCreated.getInt(0));
			actElem.setId(actId);
			actElem.setClickable(true);
			actElem.setOnClickListener(bHelper);
			
			while(mod.moveToNext()){
				RelativeLayout child = (RelativeLayout)getLayoutInflater().inflate(R.layout.rep_act_module_elem2, null);
				LinearLayout.LayoutParams paramChild = new
						LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				paramChild.leftMargin = 10;
				paramChild.rightMargin = 10;
				child.setLayoutParams(paramChild);
				
				TextView dia = (TextView)child.getChildAt(3);
				dia.setText(th.millisToHumanDay(mod.getLong(2)));
				
				TextView initHour = (TextView)child.getChildAt(2);
				initHour.setText(th.millisToHour(mod.getLong(2)));
				
				TextView endHour = (TextView)child.getChildAt(0);
				endHour.setText(th.millisToHour(mod.getLong(3)));
				
			
				
				modsParent.addView(child);
			}
			actParent.addView(actElem);
		}
	}

	public void initViews(){
		addNew = (Button)findViewById(R.id.CSchBTN_addNew);
		addNew.setOnClickListener(bHelper);	
		
		actParent = (LinearLayout)findViewById(R.id.CSchLL_actParent);
		
		/*
		LinearLayout actElem = (LinearLayout)getLayoutInflater().inflate(R.layout.create_schedule_module_elem, null);
		
		actParent.addView(actElem);
		LinearLayout actElem1 = (LinearLayout)getLayoutInflater().inflate(R.layout.create_schedule_module_elem, null);
		
		actParent.addView(actElem1);
		*/
	}
	


}
